-- setting leader key
-- 
vim.g.mapleader = " "
vim.g.maplocalleader = " "

-- 将设置快捷键设置的变量保存为本地变量
local map = vim.api.nvim_set_keymap

-- 复用opt参数
local opt = { noremap = true, silent = true }

-- map(mode, key, mapkey, options)

-- 取消S和s的默认功能
map("n", "s", "", opt)
map("n", "S", "", opt)

-- 设置保存、退出、保存并退出的快捷键
map("n", "S", ":wq<CR>", opt)
map("n", "q", ":q<CR>", opt)
map("n", "W", ":w<CR>", opt)
map("n", "qq", ":q!<CR>", opt)
map("n", "Q", ":qa!<CR>", opt)

-- 设置 分割窗口、关闭窗口等快捷键
-- 垂直分割
map("n", "sv", ":vsp<CR>", opt)
-- 水平分割
map("n", "sh", ":sp<CR>", opt)
-- 关闭当前所在窗口
map("n", "sc", "<C-w>c", opt)
-- 关闭其他窗口
map("n", "so", "<C-w>o", opt)

-- 设置 窗口之间的跳转快捷键
-- 向左边的窗口跳转
map("n", "<Leader><C-h>", "<C-w>h", opt)
-- 向右边的窗口跳转
map("n", "<Leader><C-l>", "<C-w>l", opt)
-- 向上面的窗口跳转
map("n", "<Leader><C-k>", "<C-w>k", opt)
-- 向下面的窗口跳转
map("n", "<Leader><C-j>", "<C-w>j", opt)

-- 设置窗口长宽快捷键设置
-- 左右比例控制
map("n", "<C-Left>", ":vertical resize -2<CR>", opt) -- 宽度缩小微调
map("n", "<C-Right>", ":vertical resize +2<CR>", opt) -- 宽度扩大微调
map("n", "s,", ":vertical resize -20<CR>", opt) -- 宽度缩小快调
map("n", "s.", ":vertical resize +20<CR>", opt) -- 宽度扩大快调
-- 上下比例控制
map("n", "<C-Up", ":resize -2<CR>", opt) -- 高度缩小微调
map("n", "C-Down", ":resize +2<CR>", opt) -- 高度扩大微调
map("n", "s+", ":resize +10<CR>", opt) -- 高度缩小快调
map("n", "s-", ":resize -10<CR>", opt) -- 高度扩大快调
-- 等比例
map("n", "s=", "<C-w>=", opt)

-- Terminal相关的快捷键设置
map("n", "<leader>t", ":sp | terminal<CR>", opt) -- 从水平方向分割出一个窗口作为Terminal
map("n", "<leader>vt", ":vsp | terminal<CR>", opt) -- 从垂直方向分割出一个窗口作为Terminal
map("t", "<Esc>", "<C-\\><C-n>", opt) -- 关闭Terminal
map("t", "<Leader><C-h>", [[ <C-\><C-N><C-w>h ]], opt) -- 左跳
map("t", "<Leader><C-l>", [[ <C-\><C-N><C-w>l ]], opt) -- 右跳
map("t", "<Leader><C-k>", [[ <C-\><C-N><C-w>k ]], opt) -- 上跳
map("t", "<Leader><C-j>", [[ <C-\><C-N><C-w>j ]], opt) -- 下跳

--  设置visual模式下的相关快捷键
map("v", "<", "<gv", opt) -- 向左缩进代码快
map("v", ">", ">gv", opt) -- 向右缩进代码块
map("v", "J", ":move '>+1<CR>gv-gv", opt) -- 向上选择代码块
map("v", "K", ":move '<-2<CR>gv-gv", opt) -- 向下选择代码块

-- 浏览代码快捷键设置
map("n", "<C-j>", "4j", opt) -- 向下滚动阅览
map("n", "<C-k>", "4k", opt) -- 向上滚动阅览
map("n", "<C-u>", "9j", opt) -- 向下半屏滚动阅览
map("n", "<C-d>", "9k", opt) -- 向上半屏滚动阅览

-- 在insert模式下，跳到行首和跳到行尾
map("i", "<C-h>", "<ESC>I", opt)
map("i", "<C-l>", "<ESC>A", opt)

----------------------------------------------
--//////////////////////////////////////////--
----------------------------------------------

-- 插件快捷键
-- Telescope
map("n", "<C-p>", ":Telescope find_files<CR>", opt)
map("n", "<C-f>", ":Telescope live_grep<CR>", opt)
-- bufferline
-- 左右Tab切换
map("n", "<C-h>", ":BufferLineCyclePrev<CR>", opt)
map("n", "<C-l>", ":BufferLineCycleNext<CR>", opt)
-- 关闭
-- "moll/vim-bbye"
map("n", "<C-w>", ":Bdelete!<CR>", opt)
map("n", "<Leader>bl", ":BufferLineCloseRight<CR>", opt)
map("n", "<Leader>bh", ":BufferLineCloseLeft<CR>", opt)
map("n", "<Leader>bc", ":BufferLinePickClose<CR>", opt)

local pluginKeys = {}

-- nvim-tree
-- alt + m 键打开关闭tree
map("n", "<Leader><C-m>", ":NvimTreeToggle<CR>", opt)
-- 列表快捷键
pluginKeys.nvimTreeList = {
	-- 打开文件或文件夹
	{ key = { "<CR>", "o", "<2-LeftMouse>" }, action = "edit" },
	-- 分屏打开文件
	{ key = "v", action = "vsplit" },
	{ key = "h", action = "split" },
	-- 显示隐藏文件
	{ key = "i", action = "toggle_custom" }, -- 对应 filters 中的 custom (node_modules)
	{ key = ".", action = "toggle_dotfiles" }, -- Hide (dotfiles)
	-- 文件操作
	{ key = "<F5>", action = "refresh" },
	{ key = "a", action = "create" },
	{ key = "d", action = "remove" },
	{ key = "r", action = "rename" },
	{ key = "x", action = "cut" },
	{ key = "c", action = "copy" },
	{ key = "p", action = "paste" },
	{ key = "s", action = "system_open" },
}

-- Telescope 列表中 插入模式快捷键
pluginKeys.telescopeList = {
	i = {
		-- 上下移动
		["<C-j>"] = "move_selection_next",
		["<C-k>"] = "move_selection_previous",
		["<Down>"] = "move_selection_next",
		["<Up>"] = "move_selection_previous",
		-- 历史记录
		["<C-n>"] = "cycle_history_next",
		["<C-p>"] = "cycle_history_prev",
		-- 关闭窗口
		["<C-c>"] = "close",
		-- 预览窗口上下滚动
		["<C-u>"] = "preview_scrolling_up",
		["<C-d>"] = "preview_scrolling_down",
	},
}

-- lsp 回调函数快捷键设置
pluginKeys.mapLSP = function(mapbuf)
	-- rename
	mapbuf("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opt)
	-- code action
	mapbuf("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opt)
	-- go xx
	mapbuf("n", "gd", "<cmd>Lspsaga preview_definition<CR>", opt)
	mapbuf("n", "gh", "<cmd>Lspsaga hover_doc<CR>", opt)
	mapbuf("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opt)
	mapbuf("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opt)
	mapbuf("n", "gr", "<cmd>Lspsaga lsp_finder<CR>", opt)
	-- diagnostic
	mapbuf("n", "gp", "<cmd>Lspsaga show_line_diagnostics<CR>", opt)
	mapbuf("n", "gj", "<cmd>Lspsaga diagnostics_jump_next<CR>", opt)
	mapbuf("n", "gk", "<cmd>Lspsaga diagnostics_jump_prev<CR>", opt)
	mapbuf("n", "<leader>f", ":Format<CR>", opt)
	-- 没用到
	-- mapbuf('n', '<leader>q', '<cmd>lua vim.diagnostic.setloclist()<CR>', opt)
	-- mapbuf("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opt)
	-- mapbuf('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opt)
	-- mapbuf('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opt)
	-- mapbuf('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opt)
	-- mapbuf('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opt)
end

-- nvim-cmp 自动补全
pluginKeys.cmp = function(cmp)
	return {
		-- 出现补全
		["<A-.>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
		-- 取消
		["<A-,>"] = cmp.mapping({
			i = cmp.mapping.abort(),
			c = cmp.mapping.close(),
		}),
		-- 上一个
		["<C-k>"] = cmp.mapping.select_prev_item(),
		-- 下一个
		["<C-j>"] = cmp.mapping.select_next_item(),
		-- 确认
		["<CR>"] = cmp.mapping.confirm({
			select = true,
			behavior = cmp.ConfirmBehavior.Replace,
		}),
		-- 如果窗口内容太多，可以滚动
		["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
		["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
	}
end

pluginKeys.mapVimspector = function()
  -- 开始
  map("n", "<leader>dd", ":call vimspector#Launch()<CR>", opt)
  -- 结束
  map("n", "<Leader>de", ":call vimspector#Reset()<CR>", opt)
  -- 继续
  map("n", "<Leader>dc", ":call vimspector#Continue()<CR>", opt)
  -- 设置断点
  map("n", "<Leader>dt", ":call vimspector#ToggleBreakpoint()<CR>", opt)
  map("n", "<Leader>dT", ":call vimspector#ClearBreakpoints()<CR>", opt)
  --  stepOver, stepOut, stepInto
  map("n", "<leader>dj", "<Plug>VimspectorStepOver", opt)
  map("n", "<leader>dk", "<Plug>VimspectorStepOut", opt)
  map("n", "<leader>dl", "<Plug>VimspectorStepInto", opt)
end


return pluginKeys
